package hd3.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;

import java.io.IOException;

/**
 * @Author: laoyu
 * @Date: 2020/6/4 11:07
 * @Description:
 */
public class WcJob2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //定义一个临时目录
        Path tempDir = new Path("hdfs://localhost:9000/temp");



        //1. 创建一个Job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJobName("myjob");

        //2. 关联jar不写此句代码，运行jar的时候是找不到mapper class文件
        job.setJarByClass(WcJob2.class);
        job.setNumReduceTasks(2);

        //3. 关联Mapper和 Reducer类
        //map统计
        job.setMapperClass(WcMapper.class);
        //中间统计
        job.setCombinerClass(WcReducer.class);
        //reduce统计
        job.setReducerClass(WcReducer.class);

        //4. 设置Mapper的输出key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //自定义分区
        job.setPartitionerClass(WcPartition.class);
        //指定作业输出格式，和排序作业的输入格式应该对应
        job.setOutputFormatClass(SequenceFileOutputFormat.class);

        //5. 设置最终输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //6. 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path("/calc"));
        FileOutputFormat.setOutputPath(job, new Path("/calc_output"));

        //7. 提交job
        job.waitForCompletion(true);
    }
}
